package com.jnu.myaccount.function.history;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.jnu.myaccount.MainActivity;
import com.jnu.myaccount.R;
import com.jnu.myaccount.adapter.ShowListViewAdapter;
import com.jnu.myaccount.data.Data;
import com.jnu.myaccount.data.DataBank.DataBank;
import com.jnu.myaccount.data.ShowListItem;
import com.jnu.myaccount.databinding.FragmentGalleryBinding;

import java.util.ArrayList;
import java.util.Calendar;

public class GalleryFragment extends Fragment implements View.OnClickListener {

    private GalleryViewModel galleryViewModel;
    private FragmentGalleryBinding binding;

    private ImageView selectLeft;//左箭头
    private ImageView selectRight;//右箭头
    private TextView selectYear;//年份选择

    //数据
    private  int year;//年份
    private int month;//月份
    public ArrayList<ShowListItem> showListItems=new ArrayList<>();//存放每一个ListView需要的数据（本月paid，income，以及本月的data集合）

    //listview控件
    private ListView mShowListView;
    private ShowListViewAdapter mShowListViewAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                new ViewModelProvider(this).get(GalleryViewModel.class);

        binding = FragmentGalleryBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        Calendar date = Calendar.getInstance();
        year=date.get(Calendar.YEAR);
        month=date.get(Calendar.MONTH)+1;
        //绑定控件
        selectLeft=root.findViewById(R.id.left_iv);
        selectRight=root.findViewById(R.id.right_iv);
        selectYear=root.findViewById(R.id.year_select);
        selectYear.setText(year+"年"+month+"月");
        mShowListView=root.findViewById(R.id.show_list);
        selectRight.setOnClickListener(this);
        selectLeft.setOnClickListener(this);

        getShowListItem();
        if(showListItems.size()!=0){
            mShowListViewAdapter=new ShowListViewAdapter(getActivity(),R.layout.layout_show_item,showListItems);
            mShowListView.setAdapter(mShowListViewAdapter);
                }



        return root;
    }

    public void setYear(int year,int month){
        this.year=year;
        this.month=month;
        onResume();
    }

    public void getShowListItem() {
        //生成showListItem
        ArrayList<Data> showItemData = new ArrayList<>();//临时存放同一个月的Data数组
        ArrayList<Data> sameMonthData = new ArrayList<>();//存放同一年的
        DataBank mDataBank = new DataBank(getActivity());
        if (mDataBank.loadData() != null) {
            ArrayList<Data> mListAll = mDataBank.loadData();
            for (int i = 0; i < mListAll.size(); i++) {
                if (mListAll.get(i).year == year)
                    if(mListAll.get(i).month==month)
                    sameMonthData.add(mListAll.get(i));//先存放同一yue的
            }
            if (sameMonthData.size() != 0) {
                for (int j = 1; j <= 31; j++) {//12个月份都进行遍历
                    double paid = 0.0;
                    double income = 0.0;
                    for (int k = 0; k < sameMonthData.size(); k++) {//扫描同一月的数列
                        Data g = sameMonthData.get(k);
                        if (g.day == j)//如果第j天在数组里面是存在的
                        {
                            showItemData.add(g);//把这个data放到暂存队列中
                            if (g.itemCost <= 0) {//itemcost<0说明是支出
                                paid += sameMonthData.get(k).itemCost;
                            } else {//itemcost<0说明是收入
                                income += sameMonthData.get(k).itemCost;
                            }
                        }
                    }//扫描结束后，得到同一天的data数列，以及这天的收入income以及支出paid，接下来生成一个showlistItem对象存到队列中
                    if (showItemData.size() != 0) {
                        String date = month+ "-" + j;
                        ShowListItem showListItem = new ShowListItem(date, paid, income, showItemData);
                        showListItems.add(showListItem);
                        showItemData = new ArrayList<Data>();//暂存data数据的数列清楚，为下一次扫描利用
                    }
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_iv:
                month--;
                if(month==0){
                    year--;
                    month=12;
                }
                selectYear.setText(year+"年"+month+"月");
                setYear(year,month);
                showListItems.clear();//必须保证arraylist还是原来的arraylist
                getShowListItem();
                    mShowListViewAdapter.setmList(showListItems);
                    mShowListViewAdapter.notifyDataSetChanged();

                break;
            case R.id.right_iv:
                month++;
                if(month==13){
                    year++;
                    month=1;
                }
                selectYear.setText(year+"年"+month+"月");
                setYear(year,month);
                getShowListItem();
                showListItems.clear();
                getShowListItem();
                mShowListViewAdapter.setmList(showListItems);
                mShowListViewAdapter.notifyDataSetChanged();

                break;
        }
    }
}